package com.tuozixuan;

import static com.tuozixuan.GitDemoConstants.LOCAL_DIR;

import java.io.File;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.MergeCommand;
import org.eclipse.jgit.api.MergeResult;
import org.eclipse.jgit.lib.Ref;

public class GitMergeDemo {

	public static void main(String[] args) throws Exception {
		
		Git git = Git.open(new File(LOCAL_DIR));
		String sourceBranchName = "";
		String targetBranchName = "";
		
		Ref refdev = git.checkout().setName(sourceBranchName).call(); //切换源头分支获取源头分支信息
		git.checkout().setName(targetBranchName).call();   //切换回被合并分支
		// git merge [sourceBranchName]
		MergeResult mergeResult = git.merge().include(refdev)  // 合并源头分支到目标分支
		        .setCommit(true)           // 设置合并后同时提交
		        .setFastForward(MergeCommand.FastForwardMode.NO_FF)// 分支合并策略，--ff代表快速合并， --no-ff代表普通合并
		        .setMessage("Merge sourceBranchName into targetBranchName.")     //设置提交信息
		        .call();
		System.out.println(mergeResult);
	}

}
